﻿package mortal.common.swfPlayer
{
    import flash.display.*;
    import flash.events.*;
    import flash.media.*;
    import flash.net.*;

    public class FLVPlayer extends Sprite
    {
        private var _videoURL:String = "Video.flv";
        private var _connection:NetConnection;
        private var _stream:NetStream;
        private var _video:Video;
        private var _callback:Function;
        private var _width:int;
        private var _height:int;

        public function FLVPlayer()
        {
            return;
        }// end function

        public function setSize(param1:Number, param2:Number) : void
        {
            this._width = param1;
            this._height = param2;
            if (this._video == null)
            {
                return;
            }
            this.updateSize();
            return;
        }// end function

        private function updateSize() : void
        {
            this._video.width = this._width;
            this._video.height = this._height;
            return;
        }// end function

        public function play(param1:String, param2:Function = null) : void
        {
            this._callback = param2;
            this._videoURL = param1;
            if (this._connection == null)
            {
                this._connection = new NetConnection();
            }
            if (!this._connection.hasEventListener(NetStatusEvent.NET_STATUS))
            {
                this._connection.addEventListener(NetStatusEvent.NET_STATUS, this.netStatusHandler);
                this._connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
            }
            this._connection.connect(null);
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._video != null)
            {
                this._video.clear();
            }
            if (this._stream != null)
            {
                this._stream.removeEventListener(NetStatusEvent.NET_STATUS, this.netStatusHandler);
                this._stream.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, this.asyncErrorHandler);
                this._stream.removeEventListener(Event.COMPLETE, this.loadeCompleteHandler);
                this._stream.close();
            }
            if (this._connection != null && this._connection.connected)
            {
                this._connection.removeEventListener(NetStatusEvent.NET_STATUS, this.netStatusHandler);
                this._connection.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.securityErrorHandler);
                this._connection.close();
            }
            return;
        }// end function

        private function netStatusHandler(event:NetStatusEvent) : void
        {
            switch(event.info.code)
            {
                case "NetConnection.Connect.Success":
                {
                    this.connectStream();
                    break;
                }
                case "NetStream.Play.StreamNotFound":
                {
                    break;
                }
                case "NetStream.Play.Stop":
                {
                    this.playEndHandler();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function connectStream() : void
        {
            if (this._stream == null)
            {
                this._stream = new NetStream(this._connection);
            }
            if (!this._stream.hasEventListener(NetStatusEvent.NET_STATUS))
            {
                this._stream.addEventListener(NetStatusEvent.NET_STATUS, this.netStatusHandler);
                this._stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, this.asyncErrorHandler);
                this._stream.addEventListener(Event.COMPLETE, this.loadeCompleteHandler);
            }
            if (this._video == null)
            {
                this._video = new Video();
            }
            var _loc_1:Object = {};
            _loc_1.onMetaData = this.onMetaData;
            this._stream.client = _loc_1;
            this._video.attachNetStream(this._stream);
            this._stream.play(this._videoURL);
            this.addChild(this._video);
            return;
        }// end function

        private function playEndHandler(param1 = null) : void
        {
            if (this._callback != null)
            {
                this._callback.apply(null, [this]);
            }
            return;
        }// end function

        private function loadeCompleteHandler(event:Event) : void
        {
            return;
        }// end function

        private function onMetaData(param1:Object) : void
        {
            return;
        }// end function

        private function securityErrorHandler(event:SecurityErrorEvent) : void
        {
            return;
        }// end function

        private function asyncErrorHandler(event:AsyncErrorEvent) : void
        {
            return;
        }// end function

    }
}
